//
//  BBSDetailController.m
//  CaiPiao
//
//  Created by l wb on 2018/5/21.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "BBSDetailController.h"
#import "BBSDetailCell.h"
@interface BBSDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIImageView *headImg;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UILabel *dateLab;
@property (weak, nonatomic) IBOutlet UILabel *contentLab;
@property (weak, nonatomic) IBOutlet UIImageView *showImg;
@property (weak, nonatomic) IBOutlet UILabel *likeCountLab;
@property (weak, nonatomic) IBOutlet UILabel *commentCount;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *totalComentLab;
@property (weak, nonatomic) IBOutlet UITextField *commentTF;
@property (weak, nonatomic) IBOutlet UIView *toolView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *toolHeight;
@property (strong, nonatomic) NSArray *dataArr;
@end

@implementation BBSDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"帖子详情";
    self.tableView.estimatedRowHeight = 85;
    [self setUIWithModel:self.model];

    [self getCommentDataFromNet];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
        [self registerForKeyboardNotifications];
}
-(void)getCommentDataFromNet{
    BmobQuery *query = [BmobQuery queryWithClassName:@"Comment"];
    [query whereKey:@"artitleID" equalTo:self.model.objectId];
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        NSLog(@"%@",array);
        self.dataArr = array;
        [self.tableView reloadData];
    }];
}

-(void)setUIWithModel:(BmobObject *)model{
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:[model objectForKey:@"headImgUrl"] ] placeholderImage:[UIImage imageNamed:@"avatar"]];
    [self.showImg sd_setImageWithURL:[NSURL URLWithString:[model objectForKey:@"showImg" ]] placeholderImage:[UIImage imageNamed:@"帖子占位图"]];
    self.nameLab.text = [model objectForKey:@"name"];
    self.dateLab.text = [model objectForKey:@"createdAt"];
    self.contentLab.text = [model objectForKey:@"content"];
    self.likeCountLab.text = [model objectForKey:@"likeCount"];
    self.commentCount.text = [model objectForKey:@"commentCount"];
    self.totalComentLab.text = [NSString stringWithFormat:@"共%@条评论",self.commentCount.text];
}
- (IBAction)sendCommentBtnClickAction:(UIButton *)sender {
    if ([OpenLoginManager openLoginPageWithController:self]) {

        return;
    }
    if (self.commentTF.text.length == 0) {
        [EasyTextView showText:@"请输入内容"];
        return;
    }
    BmobUser *buser = [BmobUser currentUser];
    BmobObject *object = [[BmobObject alloc] initWithClassName:@"Comment"];
    [object setObject:[buser objectForKey:@"displayName"] forKey:@"name"];
    [object setObject:[buser objectForKey:@"headImg"] forKey:@"headImgUrl"];
    [object setObject:self.commentTF.text forKey:@"comment"];
    [object setObject:self.model.objectId    forKey:@"artitleID"];
    
    [object saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            self.commentTF.text = @"";
            [self.commentTF resignFirstResponder];
            [EasyTextView showText:@"评论成功"];
            
            [self getCommentDataFromNet];
            
            NSInteger commentCount = [[self.model objectForKey:@"commentCount"] integerValue];
            commentCount= commentCount+1;
            self.commentCount.text = [NSString stringWithFormat:@"%ld",commentCount];
            self.totalComentLab.text = [NSString stringWithFormat:@"共%@条评论",self.commentCount.text];
            [self.model setObject:[NSString stringWithFormat:@"%ld",commentCount] forKey:@"commentCount"];
            [self.model updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                NSLog(@"%d",isSuccessful);
                
            }];
            
        }else{
            [EasyTextView showText:@"评论失败"];
        }
    }];
    
    NSLog(@"%@",self.commentTF.text);
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataArr) {
        return self.dataArr.count;
    }else{
        return 0;
    }
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BmobObject *object = self.dataArr[indexPath.row];
    BBSDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BBSDetailCell"];
    
    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[object objectForKey:@"headImgUrl"] ] placeholderImage:[UIImage imageNamed:@"avatar"]];
    
    
    cell.nameLab.text = [object objectForKey:@"name"];
    
    cell.dateLab.text = [object objectForKey:@"createdAt"];
    
    cell.commentLab.text = [object objectForKey:@"comment"];
    return cell;
}
#pragma mark -监听键盘高度
-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)registerForKeyboardNotifications
{
    //使用NSNotificationCenter 鍵盤出現時
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShown:)
                                                 name:UIKeyboardWillChangeFrameNotification object:nil];
    //使用NSNotificationCenter 鍵盤隐藏時
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}
//实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
- (void)keyboardWillShown:(NSNotification*)aNotification
{
    
    NSDictionary *info = [aNotification userInfo];
    
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    CGSize keyboardSize = [value CGRectValue].size;

    //输入框位置动画加载
    [UIView animateWithDuration:duration animations:^{
        
        //do something
        self.toolHeight.constant = keyboardSize.height;

    }];
    
}
//当键盘隐藏的时候
- (void)keyboardWillBeHidden:(NSNotification*)aNotification{
    //do something
    NSDictionary *info = [aNotification userInfo];
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    //输入框位置动画加载
    
    [UIView animateWithDuration:duration animations:^{
        
        //do something
        self.toolHeight.constant = 0;
        
    }];
}



@end
